package top.yi2911.lj_camerax

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.CameraSelector
import androidx.camera.core.CameraXConfig
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import top.yi2911.lj_camerax.databinding.CameraActivityMainBinding

/**
 *Author:liang jian
 *Date:2021-01-18  18:45
 *Description:
 */


class BaseCameraxActivity : AppCompatActivity() {

    lateinit var binding: CameraActivityMainBinding

    companion object {
        private const val REQUESTCODE = 1001
        private val permissions = arrayOf(
            Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
        )
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding =
            DataBindingUtil.setContentView<CameraActivityMainBinding>(this, R.layout.camera_activity_main)
        checkPermission()
    }

    /*检查相机权限*/
    private fun checkPermission() {
        this.let {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                if (checkSelfPermission()) {
                    //权限允许了
                    startCamera()
                } else {
                    //权限未允许,请求权限
                    ActivityCompat.requestPermissions(this, permissions, REQUESTCODE)
                }
            }
        }
    }

    /*检查权限是否都已经允许*/
    private fun checkSelfPermission() = permissions.all {
        ActivityCompat.checkSelfPermission(
            this,
            it
        ) == PackageManager.PERMISSION_GRANTED
    }

    //权限回调
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        when (requestCode) {
            REQUESTCODE -> {
                for (i in grantResults.indices) {
                    if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                        Toast.makeText(
                            this,
                            "您未授予${permissions[i]}权限,请打开设置授予该权限",
                            Toast.LENGTH_SHORT
                        ).show()
                        return
                    }
                }
                startCamera()
            }
        }
    }

    protected fun startCamera() {
        var future = ProcessCameraProvider.getInstance(this)
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            future.addListener({
                bindPerView(future.get())
            }, mainExecutor)
        } else {
            future.addListener({
                bindPerView(future.get())
            }, ContextCompat.getMainExecutor(this))
        }

    }

    //选择相机并绑定生命周期以及用例
    fun bindPerView(cameraProvider: ProcessCameraProvider) {
        val preview = Preview.Builder().build()
        //指定镜头方向
        val cameraSelector =
            CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_FRONT).build()
        //连接preView
        preview.setSurfaceProvider(binding.viewFinder.surfaceProvider)
        //绑定生命周期
        val camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview)

    }

    protected fun takePhoto() {}
}